package backend.product.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import core.category.model.CategoryType;
import core.product.model.Product;
import core.product.model.ProductAddress;
import core.product.model.ProductData;
import core.product.model.ProductOwner;
import core.product.model.ProductType;
import core.profile.model.BasicProfile;
import core.util.HibernateCoreUtil;

public class ProductManager extends HibernateCoreUtil
{
	public void saveProductData(ProductData pd)
	{
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.saveOrUpdate(pd);
		session.getTransaction().commit();
	}
	public Map<String,String> getProductCategoryMap(Long productId)
	{
		//select type ,name from category_product cp , category c , categorytype ct where c.uuid = cp.category_uuid and ct.uuid = c.categoryType_uuid and product_uuid = 17
		Map<String,String> categoryMap = new HashMap<String, String>();
		String sql = "select type ,name from category_product cp , category c , categorytype ct where c.uuid = cp.category_uuid and ct.uuid = c.categoryType_uuid and c.name != 'world' and product_uuid = "+ productId +" order by ct.uuid" ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		session.beginTransaction();
		SQLQuery query = session.createSQLQuery(sql).addScalar("type", Hibernate.STRING).addScalar("name", Hibernate.STRING);
		Iterator lst = query.list().iterator();
		while(lst.hasNext())
		{
			Object[] obj = (Object[]) lst.next();
			categoryMap.put((String)obj[0], (String)obj[1]);
		}
		session.getTransaction().commit();
		return categoryMap ;
	}
	public Long getProductCategoryByType(CategoryType ct,Long productId)
	{
		Long categoryId = null ;
		String sql = "select category_uuid from category_product cp,categorytype ct, category c where cp.category_uuid = c.uuid and ct.uuid = c.categoryType_uuid and c.categoryType_uuid ="+ ct.getUuid() +" and cp.product_uuid = "+productId;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		session.beginTransaction();
		SQLQuery query = session.createSQLQuery(sql).addScalar("category_uuid", Hibernate.LONG);
		List lst = query.list();
		if(lst.size() > 0)
		{
			categoryId = (Long) lst.get(0);
		}
		session.getTransaction().commit();
		return categoryId ;
	}
	public List<Long> getProductCategoriesId(Long productId)
	{
		List<Long> cateIdLst= new ArrayList<Long>();
		String sql ="select category_uuid from category_product where product_uuid="+productId;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		session.beginTransaction();
		SQLQuery query = session.createSQLQuery(sql).addScalar("category_uuid", Hibernate.LONG);
		Iterator lst = query.list().iterator();
		while(lst.hasNext())
		{
			Long cateId = (Long) lst.next();
			cateIdLst.add(cateId);
		}
		session.getTransaction().commit();
		return cateIdLst ;
	}
	public List<Product> getProductList()
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List <Product> products = session.createQuery("from Product").list();	
		session.getTransaction().commit();
		return products ;
	}
	public Product getProductByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Product p = (Product) session.get(Product.class, uuid);	
		session.getTransaction().commit();
		return p ;
	}
	@SuppressWarnings("unchecked")
	public Product getProductBySku(String sku)
	{
		Product p = null ;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Product> pLst = (List<Product>)session.createQuery("from Product p where p.sku='"+sku+"'").list();
		if(pLst.size() > 0)
		{
			p = pLst.get(0);
		}
		session.getTransaction().commit();
		return p ;
	}
	public List<ProductType> getgetAllProductType()
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List <ProductType> productType = session.createQuery("from ProductType").list();	
		session.getTransaction().commit();
		return productType ;
	}
	public void assignProductType(Product p, ProductType pt)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Set <ProductType> ptls  = p.getProductType();
		Set <ProductType> newptls  = new HashSet<ProductType>();
		Iterator <ProductType> ptlsItr  = ptls.iterator();
		while(ptlsItr.hasNext())
		{
			ProductType pttmp = ptlsItr.next();
			if(!(pt.equals(pttmp)))
			{	
				newptls.add(pttmp);
			}
		}
		newptls.add(pt);
		p.setProductType(newptls);
		session.merge(p);
		session.getTransaction().commit();
	}
	public void removeProductType(Product p, ProductType pt)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Set <ProductType> ptls  = p.getProductType();
		Set <ProductType> newptls  = new HashSet<ProductType>();
		
		Iterator <ProductType> ptlsItr  = ptls.iterator();
		while(ptlsItr.hasNext())
		{
			ProductType pttmp = ptlsItr.next();
			if(!(pt.equals(pttmp)))
			{
				
				newptls.add(pttmp);
			}
		}
		
		p.setProductType(newptls);
		session.merge(p);
		session.getTransaction().commit();	
	}
	public void saveProduct(Product p)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.merge(p);
		session.getTransaction().commit();	
	}
	public ProductAddress getProductAddress(Product p)
	{
		ProductAddress pa = null ;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductAddress> paLst = (List<ProductAddress> )session.createQuery("from ProductAddress where product=:p").setEntity("p",p).list();
		if(paLst.size() > 0)
		{
			pa = paLst.get(0);
		}
		session.getTransaction().commit();
		return pa ;
	}
	public void saveProductAddress(ProductAddress pa)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(pa);
		session.getTransaction().commit();
	}
	public ProductOwner getProductOwnerByProductAndOwner(Product p,BasicProfile owner)
	{
		ProductOwner po = null ;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductOwner> poLst = (List<ProductOwner> )session.createQuery("from ProductOwner where product=:p and owner = :owner").setEntity("p",p).setEntity("owner", owner).list();
		if(poLst.size() > 0)
		{
			po = poLst.get(0);
		}
		session.getTransaction().commit();
		return po ;
	}
	public ProductOwner getProductOwner(Product p)
	{
		ProductOwner po = null ;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductOwner> poLst = (List<ProductOwner> )session.createQuery("from ProductOwner where product=:p").setEntity("p",p).list();
		if(poLst.size() > 0)
		{
			po = poLst.get(0);
		}
		session.getTransaction().commit();
		return po ;
	}
	public void saveProductOwner(ProductOwner po)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(po);
		session.getTransaction().commit();
	}
	public ProductOwner getProductOwnerByUuid(Long uuid)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ProductOwner po = (ProductOwner) session.get(ProductOwner.class, uuid);	
		session.getTransaction().commit();
		return po ;
	}
}
